package algorithm.leetcode.I601to800;

import algorithm.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * BST的inorder traverse是递增序列
 */

public class Q783 {

    public int minDiffInBST(TreeNode root) {
        List<Integer> val = new ArrayList<>();
        inorderTraverse(root, val);
        int minInterval = Integer.MAX_VALUE;
        for (int i = 1; i < val.size(); i++) {
            minInterval = Math.min(minInterval, val.get(i)-val.get(i-1));
        }
        return minInterval;
    }

    public void inorderTraverse(TreeNode node, List<Integer> list) {
        if (node == null) return;

        inorderTraverse(node.left, list);
        list.add(node.val);
        inorderTraverse(node.right, list);
    }

}
